var app = getApp()
var onGetData = false

new Page({
    data: {
        imgArr: ['https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1506353353435&di=be662a74a9d5c115e12435226c9f7089&imgtype=0&src=http%3A%2F%2Fwww.bz55.com%2Fuploads%2Fallimg%2F120910%2F1-120910112348.jpg'],
        autoPlay: true
    },
    swiperChange(e) {
        if (e.detail.current === this.data.imgArr.length) {
            this.setData({
                autoPlay: false
            })
        }
    },
    enter() {
        if (onGetData) {
            return
        }
        onGetData = true
        var key = app.config.key
            //这里先不考虑同一台手机换多个账户的可能

        //设置钥匙
        function setKey(key) {
            app.config.key = key
            onGetData = false
            wx.setStorageSync('key', key)
            wx.redirectTo({ url: '../index/index' })
        }

        //设置新用户
        function setUser(userInfo, code) {
            app.fn.post({
                url: 'setUser',
                data: { userInfo, code },
                success(data) {
                    if (!data.code) {
                        setKey(data.key)
                    } else {
                        tryTogetKey(code)
                    }
                }
            })
        }

        //尝试获得key
        function tryTogetKey(code) {
            function getUserInfo() {
                wx.getUserInfo({
                    success(res) {
                        const userInfo = res.userInfo
                        app.fn.post({
                            url: 'getKeyByInfo',
                            data: userInfo,
                            success(data) {
                                if (!data.code) {
                                    setKey(data.key)
                                } else {
                                    setUser(userInfo, code)
                                }
                            }
                        })
                    },
                    fail() {
                        wx.showModal({
                            title: '错误',
                            content: '未能连接抽奖中心，是否重试',
                            showCancel: false,
                            success: getUserInfo
                        })
                    }
                })
            }
            getUserInfo()
        }
        //尝试登录
        function tryLogin() {
            wx.login({
                success(res) {
                    if (res.errMsg === 'login:ok') {
                        tryTogetKey(res.code)
                    } else {
                        wx.showModal({
                            title: '警告',
                            content: '未能连接抽奖中心，无法抽奖!请退出后重试',
                            showCancel: false
                        })
                        onGetData = false
                    }
                },
                fail() {
                    wx.showModal({
                        title: '警告',
                        content: '未能连接抽奖中心，无法抽奖!请退出后重试',
                        showCancel: false
                    })
                    onGetData = false
                }
            })
        }


        if (key) {
            wx.redirectTo({ url: '../index/index' })
            onGetData = false
        } else {
            app.fn.post({
                url: 'checkExist',
                success(data) {
                    if (!data.code) {
                        setKey(data.key)
                    } else {
                        tryLogin()
                    }
                }
            })
        }
    },
    onShareAppMessage() {
        return {
            success(shareTickets) {
                console.log(shareTickets)
            }
        }
    },
    onReady() {
        const bgAudio = wx.createAudioContext('bgAudio')
            // bgAudio.play()

    }
})